﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using NetSysData;

namespace BladeTecWebApplication.Admin
{
	public partial class DefineQuiz : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			NetSysData.NetSysData data = new NetSysData.NetSysData();
			QUESTION_CATEGORY[] categories = data.GetQuestionCagetories();
			QUESTION_LEVEL[] levels = data.GetQuestionLevels();
			
			ddlCategory.Items.Add(new ListItem("All", "-1"));
			foreach (QUESTION_CATEGORY cat in categories)
			{
				ddlCategory.Items.Add(new ListItem(cat.CATEGORY_NAME, cat.CATEGORY_ID.ToString()));
			}

			ddlLevel.Items.Add(new ListItem("All", "-1"));
			foreach (QUESTION_LEVEL level in levels)
			{
				ddlLevel.Items.Add(new ListItem(level.LEVEL_DESCRIPTION, level.LEVEL_ID.ToString()));
			}

			if (!Page.IsPostBack)
			{
				QUESTION[] questions = data.GetQuestions();

				foreach (QUESTION q in questions)
				{

					string tableText = "<table cellpadding=\"0\" border=\"0\" cellspacing=\"0\" width=\"980px\" id=\"questionTable" + q.QUESTION_ID + "\" class=\"catclass\" category=\"" + q.CATEGORY_ID + "\"><tr>";
					tableText += "<td syle=\"width:20px;\" valign=\"top\"><input id=\"question_" + q.QUESTION_ID + "\" type=\"checkbox\" name=\"question_" + q.QUESTION_ID + "\" value=\"\" onclick=\"toggleQuestionDiv(this.checked, "+q.QUESTION_ID+");\" /></td>";
					tableText += "<td style=\"padding-left:10px;width:680px;\">";
					tableText += q.QUESTION_TEXT + "<br />";

					if (q.QUESTION_TYPE == "M")
					{
						tableText += "<ol type=\"a\">";
						foreach (ANSWER a in q.ANSWERs)
						{
							if (a.IS_CORRECT_ANSWER == 1)
							{
								tableText += "<li>" + a.ANSWER_TEXT + " (correct)</li>";
							}
							else
							{
								tableText += "<li>" + a.ANSWER_TEXT + "</li>";
							}
						}
						tableText += "</ol>";
					}
					else
					{
						tableText += "<div style=\"padding-left:10px;\">Short Answer</div>";
					}

					tableText += "</td>";
					tableText += "<td style=\"padding-left:10px;width:100px;\" valign=\"top\">"+q.QUESTION_LEVEL.LEVEL_DESCRIPTION+"</td>";
					tableText += "<td style=\"padding-left:10px;width:200px;\" valign=\"top\">" + q.QUESTION_CATEGORY.CATEGORY_NAME + "</td>";

					tableText += "</tr></table>";

					string controlText = "<div style=\"padding: 10px;margin-bottom:10px;\" class=\"ui-widget ui-widget-content ui-corner-all\" divtype=\"questionDiv\" category=\""+q.CATEGORY_ID+"\" level=\""+q.LEVEL_ID+"\" id=\"questionDiv"+q.QUESTION_ID+"\">";
					controlText += "<div class=\"ui-dialog-content ui-widget-content\" style=\"background: none; border: 0;\" id=\"questionDivInner"+q.QUESTION_ID+"\">";
					controlText += tableText;
					controlText += "</div>";
					controlText += "</div>";
					pnlQuestions.Controls.Add(new LiteralControl(controlText));

				}

			}
		}

		protected void btnAddQuiz_Click(object sender, EventArgs e)
		{
			NetSysData.NetSysData data = new NetSysData.NetSysData();
			QUIZ quiz = new QUIZ();
			int question_id;

			quiz.ALLOW_BACKTRACK = 0;
			quiz.QUIZ_NAME = txtQuizTitle.Text;

			foreach (string s in Request.Form)
			{
				if (s.IndexOf("question_") == 0)
				{
					question_id = Int32.Parse(s.Substring(s.IndexOf("question_") + "question_".Length));
					QUIZ_QUESTION qq = new QUIZ_QUESTION();
					qq.QUESTION_ID = question_id;
					qq.QUESTION_PRIORITY = 0;
					quiz.QUIZ_QUESTION.Add(qq);
				}
			}

			data.AddQuiz(quiz);

			quizName.Text = txtQuizTitle.Text;
			pnlQuizSettings.Visible = false;
			pnlResult.Visible = true;

		}
	}
}